package org.alex.farouq.exam.view;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;

import org.alex.farouq.exam.controller.Controller;
import org.alex.farouq.exam.model.question.SubQuestion;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ScreenN1.java
 *
 * Created on Jan 31, 2012, 2:31:04 PM
 */

/**
 * 
 * @author Ahmed
 */
public class QuestionWindow extends javax.swing.JFrame {

	SubQuestionWindow subQuestionWindow;
	public Controller control;

	/** Creates new form ScreenN1 */
	public QuestionWindow() {
		subQuestionWindow = new SubQuestionWindow();
		control = Controller.getInstance();
		control.addObserver(this);
		setLocation(500, 100);
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jDesktopPane1 = new javax.swing.JDesktopPane();
		jScrollPane1 = new javax.swing.JScrollPane();
		subQuestionsList = new javax.swing.JList();
		TypeList = new javax.swing.JComboBox();
		headerText = new javax.swing.JTextField();
		Type = new javax.swing.JLabel();
		header = new javax.swing.JLabel();
		jSeparator1 = new javax.swing.JSeparator();
		jSeparator2 = new javax.swing.JSeparator();
		jSeparator3 = new javax.swing.JSeparator();
		jSeparator4 = new javax.swing.JSeparator();
		deleteButton = new javax.swing.JButton();
		editButton = new javax.swing.JButton();
		cancelCloseButton = new javax.swing.JButton();
		saveQuestionButton = new javax.swing.JButton();
		addSubQuestionButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Create Question");
		setResizable(false);
		DefaultListModel listModel = new DefaultListModel();
		subQuestionsList = new JList(listModel);

		jScrollPane1.setViewportView(subQuestionsList);

		jScrollPane1.setBounds(40, 190, 270, 200);
		jDesktopPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

		TypeList.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"1", "2", "3", "4" }));
		TypeList.setBounds(120, 70, 190, 20);
		jDesktopPane1.add(TypeList, javax.swing.JLayeredPane.DEFAULT_LAYER);

		headerText.setBounds(120, 20, 190, 30);
		jDesktopPane1.add(headerText, javax.swing.JLayeredPane.DEFAULT_LAYER);

		Type.setFont(new java.awt.Font("Tahoma", 0, 14));
		Type.setText("Type");
		Type.setBounds(40, 66, 47, 30);
		jDesktopPane1.add(Type, javax.swing.JLayeredPane.DEFAULT_LAYER);

		header.setFont(new java.awt.Font("Tahoma", 0, 14));
		header.setText("Header");
		header.setBounds(40, 13, 43, 40);
		jDesktopPane1.add(header, javax.swing.JLayeredPane.DEFAULT_LAYER);

		jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
		jSeparator1.setBounds(20, 120, 20, 340);
		jDesktopPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

		jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);
		jSeparator2.setBounds(330, 120, 10, 340);
		jDesktopPane1.add(jSeparator2, javax.swing.JLayeredPane.DEFAULT_LAYER);
		jSeparator3.setBounds(20, 460, 310, 20);
		jDesktopPane1.add(jSeparator3, javax.swing.JLayeredPane.DEFAULT_LAYER);
		jSeparator4.setBounds(20, 120, 310, 10);
		jDesktopPane1.add(jSeparator4, javax.swing.JLayeredPane.DEFAULT_LAYER);

		editButton.setText("Edit");
		// to differentiate between event sources in listener method
		editButton.setName("12");
		editButton.addActionListener(control);
		editButton.setBounds(70, 410, 73, 23);
		jDesktopPane1.add(editButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		deleteButton.setText("Delete");
		// to differentiate between event sources in listener method
		deleteButton.setName("13");
		deleteButton.addActionListener(control);
		deleteButton.setBounds(210, 410, 73, 23);
		jDesktopPane1.add(deleteButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

		cancelCloseButton.setText("Cancel / Close");
		// to differentiate between event sources in listener method
		cancelCloseButton.setName("15");
		cancelCloseButton.addActionListener(control);
		cancelCloseButton.setBounds(200, 500, 120, 23);
		jDesktopPane1.add(cancelCloseButton,
				javax.swing.JLayeredPane.DEFAULT_LAYER);

		saveQuestionButton.setText("Save Question");
		// to differentiate between event sources in listener method
		saveQuestionButton.setName("14");
		saveQuestionButton.addActionListener(control);
		saveQuestionButton.setBounds(40, 500, 120, 23);
		jDesktopPane1.add(saveQuestionButton,
				javax.swing.JLayeredPane.DEFAULT_LAYER);

		addSubQuestionButton.setFont(new java.awt.Font("Tahoma", 0, 14));
		addSubQuestionButton.setText("Add Sub_Question");
		// to differentiate between event sources in listener method
		addSubQuestionButton.setName("11");
		addSubQuestionButton.setBounds(80, 140, 190, 30);
		addSubQuestionButton.addActionListener(control);
		jDesktopPane1.add(addSubQuestionButton,
				javax.swing.JLayeredPane.DEFAULT_LAYER);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 357,
				javax.swing.GroupLayout.PREFERRED_SIZE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 578,
				Short.MAX_VALUE));

		pack();
	}// </editor-fold>

	private boolean validFields() {
		return !headerText.getText().equals("")
				&& subQuestionsList.getModel().getSize() != 0;
	}

	public boolean saveQuestionButtonActionPerformed() {
		if (!validFields()) {
			JOptionPane.showMessageDialog(this,
					"please insert data in all fields");
			return false;
		}

		setVisible(false);
		return true;
	}

	public void addEditSubQuestionItemActionPerformed(String itemName) {
		if (isEditing) {
			((DefaultListModel) subQuestionsList.getModel()).set(
					currenSelectedIndex, itemName);
			isEditing = false;
		} else {
			((DefaultListModel) subQuestionsList.getModel())
					.addElement(itemName);
		}
		subQuestionsList.updateUI();
	}

	public void addSubQuestionActionPerformed() {
		subQuestionWindow.setVisible(true);
	}

	public void cancelCloseButtonActionPerformed() {
		setVisible(false);
		subQuestionWindow.setVisible(false);
	}

	public int deleteButtonActionPerformed() {
		int index = subQuestionsList.getSelectedIndex();
		if (index != -1) {
			((DefaultListModel) subQuestionsList.getModel()).remove(index);
			subQuestionsList.updateUI();
		} else {
			JOptionPane.showMessageDialog(this,
					"please select an item to delete");
		}
		return index;
	}

	public int editButtonActionPerformed() {
		int index = subQuestionsList.getSelectedIndex();
		if (index != -1) {
			setEditing(true, index);
		} else {
			JOptionPane
					.showMessageDialog(this, "please select an item to edit");
		}
		return index;
	}

	public void setSubQuestionToBeEdited(String header, int typeIndex, String mark, String duration ,String [] answers) {

		subQuestionWindow.showEditedSubQuestion(header, typeIndex, mark, duration ,answers);
		subQuestionWindow.setVisible(true);

	}

	private void setEditing(boolean b, int index) {
		isEditing = b;
		currenSelectedIndex = index;
	}

	public String getHeaderText() {
		return headerText.getText();
	}

	public void showEditedQuestion(String header, int typeIndex, String [] subQuestions) {
		headerText.setText(header);
		TypeList.setSelectedIndex(typeIndex);
		
		((DefaultListModel) subQuestionsList.getModel()).removeAllElements();
		for (String answer : subQuestions) {
			((DefaultListModel) subQuestionsList.getModel())
			.addElement(answer);
		}
		subQuestionsList.updateUI();
	}
	// Variables declaration - do not modify
	private javax.swing.JLabel Type;
	private javax.swing.JComboBox TypeList;
	private javax.swing.JButton addSubQuestionButton;
	private javax.swing.JButton cancelCloseButton;
	private javax.swing.JButton deleteButton;
	private javax.swing.JButton editButton;
	private javax.swing.JLabel header;
	private javax.swing.JTextField headerText;
	private javax.swing.JDesktopPane jDesktopPane1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JSeparator jSeparator2;
	private javax.swing.JSeparator jSeparator3;
	private javax.swing.JSeparator jSeparator4;
	private javax.swing.JButton saveQuestionButton;
	private javax.swing.JList subQuestionsList;
	// End of variables declaration
	private boolean isEditing;
	private int currenSelectedIndex;;
}
